package com.biz.primus.ms.oms.service.bridge;

import com.biz.primus.common.enums.StockChannelEnum;
import com.biz.primus.model.stock.enums.StockChangeTypeEnum;
import com.biz.primus.model.stock.vo.IncrDepotStockReqVo;
import com.biz.primus.model.stock.vo.ProductQuantityPair;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.oms.dao.po.returns.OmsReturn;
import com.biz.primus.ms.oms.dao.po.returns.OmsReturnItem;
import com.biz.primus.ms.oms.dao.repository.OmsOrderReturnRepository;
import com.biz.primus.ms.oms.feign.StockFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yanweijin
 * @date 2017/11/17
 */
@Service
public class BridgeServiceImpl extends AbstractBaseService implements BridgeService{

	@Autowired
	private OmsOrderReturnRepository omsOrderReturnRepository;

	@Autowired
	private StockFeignClient stockFeignClient;
	@Override
	public void stockChange(Long returnId) {
		IncrDepotStockReqVo incrDepotStockReqVo = new IncrDepotStockReqVo();

        OmsReturn omsReturn = omsOrderReturnRepository.findOne(returnId);
        List<OmsReturnItem> omsReturnItems =omsReturn.getItems();
        List<ProductQuantityPair> productQuantityPairs = omsReturnItems.stream().map(req->{
               ProductQuantityPair productQuantityPair =new ProductQuantityPair();
               productQuantityPair.setQuantity(req.getQuantity());
               productQuantityPair.setProductCode(req.getProductCode());
                return productQuantityPair;
            }).collect(Collectors.toList());

           incrDepotStockReqVo.setItems(productQuantityPairs);
           incrDepotStockReqVo.setBn(omsReturn.getReturnCode());
           incrDepotStockReqVo.setChangeType(StockChangeTypeEnum.RETURN);
           incrDepotStockReqVo.setDepotCode(omsReturn.getDepotCode());
           incrDepotStockReqVo.setChannelCode(StockChannelEnum.NORMAL);
           stockFeignClient.updateStockOnly(incrDepotStockReqVo);
	}
}
